<?php


namespace app\admin\controller;


use app\admin\business\AdminUser;
use think\facade\View;

class Login extends AdminBase
{
    public function initialize()
    {
        if($this->isLogin()){
            return $this->redirect(url('index/index'));
        }
    }

    public function index(){
        return View::fetch();
    }
    public function md5(){
        halt(session(config('admin.session_admin')));//查看用户session是否存储成功
        echo md5("123456_tianQi_abc");
    }
    public function check(){
        if(!$this->request->isPost()){
            return show(config("status.error"), "请求方式错误");
        }
        // 参数校验：1 原生方式 2 TP 验证机制
        $username = $this->request->param("username", "", "trim");
        $password = $this->request->param("password", "", "trim");
        $captcha = $this->request->param("captcha", "", "trim");
        $validate = new \app\admin\validate\AdminUser();
        $data = [
            "username" => $username,
            "password" => $password,
            "captcha" => $captcha
        ];
        if(!$validate->check($data)){
            return show(config("status.error"), $validate->getError());
        }
        /*if(empty($username) || empty($password) || empty($captcha)){
            return show(config("status.error"), "参数不能为空");
        }
        // 验证码是否正确
        if(!captcha_check($captcha)){
            return show(config("status.error"), "验证码不正确");
        }*/
        try {
            $result = AdminUser::login($data);
        } catch (\Exception $e){
            return show(config("status.error"), $e->getMessage());
        }
        if($result){
            return show(config("status.success"), "登录成功");
        } else {
            return show(config("status.error"), "登录失败");
        }
    }
}
